var user

export default {
  /**
   * 设置用户数据
   */
  setUser (_user) {
    user = _user
  },
  /**
   * 返回用户数据
   */
  getUser () {
    if (!user) {
      this.setUser(JSON.parse(localStorage.getItem('tradeUser')))
    }
    return user
  },
  /**
   * 检测用户权限
   */
  checkUserPermission (permissions) {
    // return true;
    var result = false
    var requiredPermissions
    var i
    var j
    var _user = this.getUser()
    if (typeof permissions === 'string') {
      // debugger
      // 参数是一个权限，即字符串
      requiredPermissions = []
      requiredPermissions.push(permissions)
    } else {
      // 参数是一个权限数组，即字符串数组
      requiredPermissions = permissions
    }
    if (!requiredPermissions || requiredPermissions.length === 0) {
      result = true
    } else {
      for (i in requiredPermissions) {
        for (j in _user.authorities) {
          if (requiredPermissions[i] === _user.authorities[j]) {
            result = true
            break
          }
        }
      }
    }
    return result
  }
}
